package demo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/***
 * 使用servlet分三步：
 * 1、继承HttpServlet
 * 2、重写doGet()或者DoPost()方法，使用哪种方式就需要重写哪种
 * 3、在web.xml中注册servlet
 */
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //后台输出内容
        System.out.println("处理get()请求...");
        //给浏览器输出
        PrintWriter out = resp.getWriter();
        //指定输出的文件类型
        resp.setContentType("text/html;charset=utf-8");
        out.println("<strong>Hello Servlet!</strong><br>");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //后台输出内容
        System.out.println("处理post()请求...");
        //给浏览器输出
        PrintWriter out = resp.getWriter();
        //指定输出的文件类型
        resp.setContentType("text/html;charset=utf-8");
        out.println("<strong>Hello Servlet!</strong><br>");
    }
}
